package com.wanjia.bpsapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanjia.bpsapi.annotation.LoginToken;
import com.wanjia.bpsapi.pojo.dto.ParamName;
import com.wanjia.bpsapi.pojo.entity.Param;
import com.wanjia.bpsapi.pojo.model.JsonResult;
import com.wanjia.bpsapi.service.IParamService;
import com.wanjia.bpsapi.util.JsonResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cost")
@Tag(name = "成本项维护接口控制器")
public class CostController extends BaseController {

    @Setter(onMethod_ = @Autowired)
    private IParamService service;

    @Operation(summary = "获取所有材料名称接口")
    @GetMapping("/names")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<List<ParamName>> getParamNames() {
        return JsonResultUtil.success(service.getParamNames());
    }

    @Operation(summary = "查询成本项接口（分页）")
    @GetMapping
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Page<Param>> pageParams(
            @Parameter(description = "页码") Integer pageNo,
            @Parameter(description = "页尺寸") Integer pageSize,
            @Parameter(description = "成本项id") @RequestParam(required = false) Integer paramId) {
        return JsonResultUtil.success(service.pageParams(pageNo, pageSize, paramId));
    }

    @Operation(summary = "新增成本项参数接口")
    @PostMapping
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Param> addParam(@RequestBody Param param) {
        return JsonResultUtil.success(service.addParam(param));
    }

    @Operation(summary = "修改成本项参数接口")
    @PutMapping
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Param> updateParam(@RequestBody Param param) {
        return JsonResultUtil.success(service.updateParam(param));
    }

    @Operation(summary = "获取最新菜品勾选的成本项")
    @GetMapping("/latest")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<List<Long>> getParamIdByVegeId(
            @RequestParam(required = false) Long vegeId,
            @RequestParam(required = false) Long uid) {
        return JsonResultUtil.success(service.getLatestParamIds(vegeId, uid));
    }

}
